package com.xianyu.us.service.impl;


import java.util.List;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xianyu.us.common.annotatian.ExtCacheable;
import com.xianyu.us.common.exception.ServiceException;
import com.xianyu.us.common.utils.ShiroUtils;
import com.xianyu.us.common.vo.PageObject;
import com.xianyu.us.dao.UsGoodsDao;
import com.xianyu.us.pojo.entity.UsGoods;
import com.xianyu.us.pojo.entity.UsOrder;
import com.xianyu.us.service.UsGoodsService;
/**
 * 发布商品发布业务实现
 * @author lixilin
 *
 */
@Service
public class UsGoodsServiceImpl implements UsGoodsService {
	@Autowired
	private UsGoodsDao usGoodsDao;
	
	/**
	 * 根据ID查询已发布的商品
	 */
	@Override
	public List<UsGoods> findById(Integer id) {
		if(id==null)
			throw new IllegalArgumentException("改商品不存在");
		List<UsGoods> records = usGoodsDao.findById(id);
		return records;
	}
	/**
	 * 删除选中的商品
	 */
	@Override
	public int deleteById(Integer id) {
		return usGoodsDao.deleteById(id);
	}
	
	/**
	 * 根据用户ID查询已发布的商品
	 */
	@Override
	public PageObject<UsGoods> findByUserId(Integer id,Integer pageCurrent) {
		if(id==null)
			throw new IllegalArgumentException("用户不存在");
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("当前页不合法");
		List<UsGoods> records = usGoodsDao.findByUserId(id);
		int pageSize=3;
		Page<UsGoods> page=PageHelper.startPage(pageCurrent, pageSize);
		return new PageObject<>(pageCurrent,pageSize, (int)page.getTotal() ,(int)page.getPages(),records);
	}
	
	/**
	 * 更改发布商品信息
	 */
	@Override
	public int updateById(UsGoods entity) {
		if(entity==null)
			throw new IllegalArgumentException("商品不存在");
		int rows = usGoodsDao.updateById(entity);
		return rows;
	}
	
	/**
	 * 首页查询数据业务
	 */
	@Cacheable(value = "index") //缓存热点数据，默认以输入参数做为键
	@Override
	public PageObject<UsGoods> finObject(String content,Integer pageCurrent) {
		if(pageCurrent==null || pageCurrent<1)
			throw new IllegalArgumentException("当前页码无效！");
		
		int pageSize=8;//每行显示6个，显示两行
		Page<UsGoods> page=PageHelper.startPage(pageCurrent, pageSize);
		
		List<UsGoods> records = usGoodsDao.findObject(content);

		return new PageObject<UsGoods>(pageCurrent,pageSize,(int)page.getTotal(),(Integer)page.getPages(),records);
	
			 
	}

	
	@CacheEvict(value = "index",allEntries = true)//做了插入就要清除缓存
	@Override
	public int insertGoods(UsGoods entity,MultipartFile... file) {
		//设置发布用户id
		entity.setUserId(ShiroUtils.getId());
		//合法性校验
		if(StringUtils.isEmpty(entity) || StringUtils.isEmpty(file))
			throw new IllegalArgumentException("保存的商品对象或图片不能为空");
		//解析图片数据
		String imgUrl="";

		for(int i =0;i<file.length;i++) {
			MultipartFile upfile=file[i];
			String path="\\images\\common\\";
			String string = UUID.randomUUID().toString();//不会重复的
			String originalFilename = upfile.getOriginalFilename();
			String substring = 
					originalFilename.substring(originalFilename.lastIndexOf("."));//截取后缀名
			String fileName =string+ substring;//UUID+图片后缀名
			try {
				String canonicalPath = new File("").getCanonicalPath();//获取项目的根路径
				
				String parentMkdirs = new File(canonicalPath).getParent();//获取项目根路径的上一级目录
				File newfile = new File(parentMkdirs+path+fileName);
				File newMkdirs = new File(parentMkdirs+path);

				if(!newMkdirs.exists()) {
					System.out.println("创建文件夹");
					newMkdirs.mkdirs();
				}

				imgUrl+="/images/common/"+fileName+";";//保存文件的想对路径
				upfile.transferTo(newfile);//复制图片,拷贝
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
				throw new ServiceException("图片保存失败,请重试");
			}

		}
		entity.setImgUrl(imgUrl);//写入图片路径
		//System.out.println(imgUrl);
		int rows=usGoodsDao.insertGoods(entity);
		if(rows<1)
			throw new ServiceException("图片保存失败,请重试");
		return rows;

	}
	
	/**
	 * 桂喜。通过买方id查询订单信息
	 */
	@Override
	public List<UsOrder> findByGoods(Integer id) {

		return usGoodsDao.findByGoods(id);
	}
}


